package utils;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Config {
    private int id;
    private String name;//项目的名称，方便查看
    private String type;//根据项目type在Listener选择数据处理的方式
    private int port;
    private String portType;//UDP or TCP
    private String driverClassName;
    private String url;
    private String userName;
    private String password;
    private String sql;
    
    private static final Logger logger = LogManager.getLogger(Config.class);
    
    public Config() {
    }

    public Config(int id, String name, String type, int port, String portType, String driverClassName, String url,
            String userName, String password, String sql) {
        this.id = id;
        this.name = name;
        this.type = type;
        this.port = port;
        this.portType = portType;
        this.driverClassName = driverClassName;
        this.url = url;
        this.userName = userName;
        this.password = password;
        this.sql = sql;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getPortType() {
        return portType;
    }

    public void setPortType(String portType) {
        this.portType = portType;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
    

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }
    
    @Override
    public String toString() {
        return "Config [id=" + id + ", name=" + name + ", type=" + type + ", port=" + port + ", portType=" + portType
                + ", driverClassName=" + driverClassName + ", url=" + url + ", userName=" + userName + ", password="
                + password + ", sql=" + sql + "]";
    }

    public static List<Config> loadConfig(InputStream in){
        List<Config> configList = new ArrayList<Config>();
        Scanner sc = null;
        Scanner lineScanner = null;
        try{
            sc = new Scanner(in);
            String line = null;
            while(sc.hasNextLine()){
                try {
                    line = sc.nextLine();
                    Config config = null;
                    config = new Config();
                    lineScanner = new Scanner(line);
                    config.id = lineScanner.nextInt();
                    config.name = lineScanner.next().trim();
                    config.type = lineScanner.next().trim();
                    config.port = lineScanner.nextInt();
                    config.portType = lineScanner.next().trim().toUpperCase();
                    config.driverClassName = lineScanner.next().trim();
                    config.url = lineScanner.next().trim();
                    config.userName = lineScanner.next().trim();
                    config.password = lineScanner.next().trim();
                    config.sql = lineScanner.nextLine().trim();
                    checkConfig(config, configList);
                    logger.debug(config);
                    configList.add(config);
                } catch (Exception e) {
                    logger.error(e+" line:"+line);
                }
            }
        }finally{
            sc.close();
            if(lineScanner!=null)
                lineScanner.close();
        }
        
        return configList;
    }

    private static void checkConfig(Config config, List<Config> configList) {
        
        if(config.port<1 || config.port>65535)
            throw new IllegalArgumentException("id:"+config.id+" error:端口号超出范围, port="+config.port);
        if(!config.portType.equalsIgnoreCase("TCP") && !config.portType.equalsIgnoreCase("UDP"))
            throw new IllegalArgumentException("id:"+config.id+" error:端口类型只能为UDP或TCP, type="+config.portType);
        for (Config e : configList) {
            if(e.id == config.id)
                throw new IllegalArgumentException("id:"+config.id+" error:id重复, id="+config.id);
            if(e.port == config.port)
                throw new IllegalArgumentException("id:"+config.id+" error:端口号重复, port:"+config.port);
        }
        
    }
    
    
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((driverClassName == null) ? 0 : driverClassName.hashCode());
        result = prime * result + ((password == null) ? 0 : password.hashCode());
        result = prime * result + ((url == null) ? 0 : url.hashCode());
        result = prime * result + ((userName == null) ? 0 : userName.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Config other = (Config) obj;
        if (driverClassName == null) {
            if (other.driverClassName != null)
                return false;
        } else if (!driverClassName.equals(other.driverClassName))
            return false;
        if (password == null) {
            if (other.password != null)
                return false;
        } else if (!password.equals(other.password))
            return false;
        if (url == null) {
            if (other.url != null)
                return false;
        } else if (!url.equals(other.url))
            return false;
        if (userName == null) {
            if (other.userName != null)
                return false;
        } else if (!userName.equals(other.userName))
            return false;
        return true;
    }

   
}
